<!--
 * @Description:
 * @Author: 陈-
 * @Date: 2021-06-24 15:58:34
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-06-27 21:01:54
-->
<template>
  <!-- 轮播配置页面 -->
  <div>
    <el-button type="primary" @click="toAdd">新增</el-button>
    <el-button type="primary" @click="toSee">预览</el-button>
    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column
        align="center"
        label="序号"
        type="index"
      />
      <el-table-column align="center" label="轮播图" prop="name" />
      <el-table-column align="center" label="轮播图描述" prop="introduce" />
      <el-table-column align="center" label="轮播图图片">
        <template scope="scope">
          <el-popover placement="right" width="200" trigger="hover">
            <img :src="scope.row.url" width="200" height="160">
            <img slot="reference" :src="scope.row.url" width="40" height="40">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '正常' ? 'success' : 'danger'">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <!-- <keep-alive> -->
          <el-button
            v-if="scope.row.status == '正常'"
            size="mini"
            @click="toChangeStatu(scope.row)"
          >停用</el-button>
          <el-button
            v-else
            size="mini"
            @click="toChangeStatu(scope.row)"
          >启用</el-button>
          <!-- </keep-alive> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 轮播图 -->
    <el-dialog title="大屏预览" :visible.sync="visible1" width="80%">
      <span><el-carousel :interval="5000" arrow="always">
        <el-carousel-item
          v-for="item in tableData"
          :key="item.id"
          :data="tableData"
        >
          <img :src="item.url" width="100%" height="300px">
        </el-carousel-item> </el-carousel></span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="visible1 = false">关闭</el-button>
      </span>
    </el-dialog>
    <!-- 模态框 -->
    <el-dialog title="新增轮播图" :visible.sync="visible">
      <!-- {{ form }} -->
      <el-form ref="ruleForm" :model="form" :rules="rules">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="轮播图片" :label-width="formLabelWidth" prop="url">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input
            v-model="form.introduce"
            type="textarea"
            :rows="2"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  carousel_findAll,
  carousel_deleteById,
  // carousel_query,
  carousel_saveOrUpdate
} from '@/api/carousel'
import { showFileURL, uploadFileURL } from '@/utils/config'
// import { log } from '@antv/g2plot/lib/utils'
export default {
  data() {
    return {
      tableData: [],
      form: {},
      visible: false,
      visible1: false,
      formLabelWidth: '80px',
      imageUrl: '',
      showFileURL,
      uploadFileURL,
      autosize: {},
      ruleForm: '',
      rules: {
        name: [
          {
            required: true,
            message: '请输入轮播图名称',
            trigger: 'blur'
          }
        ],
        url: [
          {
            required: true,
            message: '请上传轮播图',
            trigger: 'change'
          }
        ]
      }
    }
  },
  computed: {},
  created() {
    this.findAll()
  },
  mounted() {},
  methods: {
    tips() {
      this.$message({
        message: '更新成功',
        type: 'success'
      })
    },
    async findAll() {
      const res = await carousel_findAll()
      this.tableData = res.data
      console.log(this.tableData)
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.url = this.showFileURL + res.data.id
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    toAdd() {
      this.visible = true
      this.form = {}
      this.imageUrl = ''
    },
    toSave() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          // 校验通过，做自己的业务，保存
          // console.log("校验通过");
          // this.form;
          await carousel_saveOrUpdate(this.form)
          // console.log(res, "-----");
          // 提示用户保存成功，刷新表格数据，关闭模态框
          this.tips()
          this.findAll()
          // this.toClose();
          this.visible = false
        } else {
          // 校验不通过
          return false
        }
      })
    },
    async toChangeStatu(form) {
      console.log(123)
      form.status = form.status === '正常' ? '停用' : '正常'
      await carousel_saveOrUpdate(form)
      this.findAll()
      // 提示
      this.tips()
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await carousel_deleteById(id)
          this.findAll()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleEdit(data) {
      console.log(data)
      this.visible = true
      this.form = { ...data }
      this.imageUrl = this.form.url
    },
    toSee() {
      this.visible1 = true
    }
  }
}
</script>
<style scoped></style>
